//
//  HJPayTransferDetailController.m
//  KaiChat
//
//  Created by lx on 2024/1/31.
//

#import "HJPayTransferDetailController.h"
#import "HJPayTransferDetailItem.h"
#import "QMUIButton.h"

@interface HJPayTransferDetailController ()

@property (nonatomic, strong) NSDictionary *orderInfo;

@end

@implementation HJPayTransferDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"Transfer Details";
    
    if (!_orderInfo) {
        self.orderInfo = self.messageModel.msg.mj_JSONObject[@"content"];
    }

    //如果是自己就是减号
    
    UIScrollView *scrollView = [UIScrollView addToView:self.view];
    scrollView.frame = CGRectMake(0, KNavBarHight, KScreenWidth, KScreenHeight - KNavBarHight);
    scrollView.backgroundColor = UIColor.tj_backgroundColor;
    CGSize contentSize = scrollView.bounds.size;
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    scrollView.contentSize = contentSize;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.alwaysBounceVertical = YES;
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    
    UIView *contentView = [[UIView alloc] init].addTo(scrollView);
    contentView.backgroundColor = UIColor.tj_backgroundColor;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
        make.width.offset(contentSize.width);
        make.height.offset(contentSize.height);
    }];
    
    
    
    BOOL isSelf = TJIM_isSelf(self.messageModel.senderId);

    NSLog(@"orderInfo = %@, messageModel = %@", _orderInfo, self.messageModel);
    
    UIImageView *coinImageView = [UIImageView addToView:contentView];
    coinImageView.image = [UIImage imageNamed:@"USDT_icon"];
    coinImageView.addCornerRadius(20);
    [coinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(40);
        make.centerX.offset(0);
        make.top.offset(10);
    }];
    
    UILabel *amountLabel = [UILabel lableWithSuperView:contentView font:UIFontMediumMake(22) color:UIColor.tj_333333Color title:[NSString stringWithFormat:@"%@%@", isSelf ? @"-" : @"+", Dividing_powl(self.orderInfo[@"value"], 10, 6)] textAlignment:NSTextAlignmentLeft];
    [amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(coinImageView.mas_bottom).offset(12);
    }];
    
//    UILabel *typeLabel = [UILabel lableWithSuperView:contentView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:self.orderInfo[@"title"] textAlignment:NSTextAlignmentLeft];
//    [typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.offset(0);
//        make.top.equalTo(amountLabel.mas_bottom).offset(8);
//    }];
//    
    QMUIButton *stateButton = [QMUIButton buttonWithSuperView:contentView font:UIFontMake(14) color:UIColor.tj_333333Color title:nil];
    stateButton.userInteractionEnabled = NO;
    NSInteger orderStatus = self.messageModel.orderStatus;
    if (orderStatus == 3) {
        [stateButton setTitle:@"Confirmed" forState:UIControlStateNormal];
        [stateButton setImage:[UIImage imageNamed:@"pay_status_complete"] forState:UIControlStateNormal];
        [stateButton setTitleColor:UIColor.tj_3AB197Color forState:UIControlStateNormal];
    } else if (orderStatus == 4) {
        [stateButton setTitle:@"Failed" forState:UIControlStateNormal];
        [stateButton setImage:[UIImage imageNamed:@"pay_status_error"] forState:UIControlStateNormal];
        [stateButton setTitleColor:UIColor.tj_E0421CColor forState:UIControlStateNormal];
    } else {
        [stateButton setTitle:@"Pending" forState:UIControlStateNormal];
        [stateButton setImage:[UIImage imageNamed:@"pay_status_pending"] forState:UIControlStateNormal];
        [stateButton setTitleColor:UIColor.tj_FFA41AColor forState:UIControlStateNormal];
    }
    [stateButton setImagePosition:QMUIButtonImagePositionLeft];
    stateButton.spacingBetweenImageAndTitle = 4;
    [stateButton sizeToFit];
    [stateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.height.offset(stateButton.tj_height);
        make.width.offset(stateButton.tj_width);
        make.top.equalTo(amountLabel.mas_bottom).offset(8);
    }];
 
    UILabel *transferTitleLabel = [UILabel lableWithSuperView:contentView font:UIFontSemiboldMake(14) color:UIColor.tj_333333Color title:@"Transaction" textAlignment:NSTextAlignmentLeft];
    [transferTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.inset(leftRightMargin);
        make.top.equalTo(stateButton.mas_bottom).offset(30);
    }];
    
    UIView *lineView = [UIView addToView:contentView];
    lineView.backgroundColor = UIColor.tj_separatorColor;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.top.equalTo(transferTitleLabel.mas_bottom).offset(10);
        make.height.offset(lineHeight);
    }];
    
    HJPayTransferDetailItem *personItem = [HJPayTransferDetailItem addToView:contentView];
    personItem.textLabel.text = isSelf ? @"To" : @"From";
    personItem.detailTextLabel.text = isSelf ? self.sessionModel.sessionName :  HJUserInfo.nickname;
    [personItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(lineView.mas_bottom).offset(15);
    }];
    
    HJPayTransferDetailItem *tokenItem = [HJPayTransferDetailItem addToView:contentView];
    tokenItem.textLabel.text = @"Token";
    tokenItem.detailTextLabel.text = _orderInfo[@"pay_coin"];
    [tokenItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(personItem.mas_bottom).offset(15);
    }];
    
    HJPayTransferDetailItem *typeItem = [HJPayTransferDetailItem addToView:contentView];
    typeItem.textLabel.text = @"Type";
    if ([_orderInfo[@"pay_type"] intValue] == 1) {
        typeItem.detailTextLabel.text = @"Transfer";
    }
    [typeItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(tokenItem.mas_bottom).inset(15);
    }];
    
    HJPayTransferDetailItem *dateItem = [HJPayTransferDetailItem addToView:contentView];
    dateItem.textLabel.text = @"Date";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    dateItem.detailTextLabel.text = [NSString dateStringWithDateFormat:formatter timeInterval:[_orderInfo[@"created_at"] floatValue]];
    [dateItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(typeItem.mas_bottom).inset(15);
    }];
    
    HJPayTransferDetailItem *orderItem = [HJPayTransferDetailItem addToView:contentView];
    orderItem.textLabel.text = @"Order number";
    orderItem.detailTextLabel.text = _orderInfo[@"out_trade_order_id"];
    [orderItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(dateItem.mas_bottom).inset(15);
    }];
    
    
}

 

@end
